home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 202_01 / ked2.c < prev    next >
Text File  |  1980-01-01  |  25KB  |  712 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <ked.h>
  4.  
  5.  
  6. getchr()   /* low-level character input */
  7. {
  8.      char c;
  9.  
  10.      c = getch();
  11.      putch(c);
  12.      return c;
  13. }
  14.  
  15.  
  16. int lower(c)
  17. char c;
  18. {
  19.      return isupper(c) ? c+32 : c;
  20. }
  21.  
  22. int upper(c)
  23. char c;
  24. {
  25.      return islower(c) ? c-32 : c;
  26. }
  27.  
  28.  
  29. readin(w)   /* find location of command in comm array or symbol table */
  30. char w[];
  31. {
  32.           int c,i,l,m;
  33.  
  34.           do {
  35.             inputs(w);
  36.             if ((m = match(w,comm,MAXCOMM)) != NOMATCH)
  37.                return m;              
  38.             if (m == NOMATCH && last != first && p != pr) {
  39.                l = pr->lastin;
  40.                if (l == 15) 
  41.                    l = last;
  42.                for (i = first; i != l; i = new[i].nextin)
  43.                     if ((c = strcomp(w,new[i].defname)) == 0) 
  44.                        return ( -i - 1);
  45.             }          
  46.             printf("INVALID COMMAND. TYPE MN TO SEE MENU.\n?");
  47.           } while (m == NOMATCH);
  48. }
  49.  
  50.  
  51.  
  52. menu()           /* print updated menu */
  53. {
  54.           int i;
  55.  
  56.           printf("THE FOLLOWING IS A LIST OF VALID ");
  57.           printf("COMMANDS FOR THIS EDITOR.\n\n");
  58.           printf("  INSTRUCTIONS\n");   
  59.           printf("m          move\n");
  60.           printf("tl         turnleft\n");
  61.           printf("pi         pickbeeper\n");
  62.           printf("pu         putbeeper\n");
  63.           printf("to         turnoff\n");
  64.           printf("b          BEGIN\n");
  65.           printf("bx         BEGINNING-OF-EXECUTION\n");
  66.           printf("nd         END\n");
  67.           printf("ndc        end construct\n");
  68.           printf("ndx        END-OF-EXECUTION\n");
  69.           printf("if         IF\n");
  70.           printf("el         ELSE\n");
  71.           printf("it         ITERATE\n");
  72.           printf("wh         WHILE\n");
  73.           printf("df         DEFINE-NEW-INSTRUCTION\n");
  74.           printf("\nTYPE ANY KEY TO CONTINUE.\n");
  75.           getchr();
  76.           if (last != first) {   /* any valid sym tab entries? */
  77.            printf("\n  USER-DEFINED COMMANDS\n");
  78.            for (i = first; i != last; i = new[i].nextin)
  79.                 printf("%s\n", new[i].defname);  
  80.           }
  81.           printf("\n  EDIT COMMANDS\n");      
  82.           printf("n          move pointer to next instruction\n");
  83.           printf("nc         move pointer to next construct\n");
  84.           printf("n-         move pointer to previous instruction\n");
  85.           printf("n--        move pointer back 10 lines\n"); 
  86.           printf("d          delete current instruction\n");
  87.           printf("c          print current line\n");
  88.           printf("p          print next 20 lines\n");
  89.           printf("i          insert new instruction before current one\n");
  90.           printf("t          move pointer to the first line\n");
  91.           printf("l          list the rest of the program\n");
  92.           printf("s          save the program\n");
  93.           printf("mn         print an updated menu\n");
  94.           printf("q          quit the edit program\n");
  95.           printf("\nTYPE ANY KEY TO CONTINUE.\n");
  96.           getchr();
  97.           printf("\n  TESTS\n");
  98.           printf("n          facing-north\n");
  99.           printf("nn         not-facing-north\n");
  100.           printf("s          facing-south\n");
  101.           printf("ns         not-facing-south\n");
  102.           printf("e          facing-east\n");
  103.           printf("ne         not-facing-east\n");
  104.           printf("w          facing-west\n");
  105.           printf("nw         not-facing-west\n");
  106.           printf("c          next-to-a-beeper (beeper on corner)\n");
  107.           printf("nc         not-next-to-a-beeper\n");
  108.           printf("b          any-beepers-in-beeper-bag\n");
  109.           printf("nb         no-beepers-in-beeper bag\n");
  110.           printf("f          front-is-clear\n");
  111.           printf("nf         front-is-blocked\n");
  112.           printf("l          left-is-clear\n");
  113.           printf("nl         left-is-blocked\n");
  114.           printf("r          right-is-clear\n");
  115.           printf("nr         right-is-blocked\n");
  116. }
  117.  
  118. createtree()                      /* reads information from old file1 */
  119. {
  120.            int i,in,d,fn,fs,b,t,l;
  121.            char *malloc();
  122.  
  123.            fscanf(fptr1, "%d %d %d %d %d %d %d %d",
  124.                   &i,&in,&d,&fn,&fs,&b,&t,&l);
  125.            pr = NULL;
  126.            p = (struct tnode *)malloc(24);
  127.            tree(i,in,d,fn,fs,b,t,l);              /* store first node */
  128.            root = p;
  129.            p = root->sub = (struct tnode *)malloc(24);
  130.            pr = root;
  131.            fscanf(fptr1, "%d %d %d %d %d %d %d %d",
  132.                   &i,&in,&d,&fn,&fs,&b,&t,&l);
  133.            while (i != NDX && i != CPMEOF && i != EOF) { 
  134.                createnode(fptr1,i,in,d,fn,fs,b,t,l);    
  135.                fscanf(fptr1, "%d %d %d %d %d %d %d %d",
  136.                       &i,&in,&d,&fn,&fs,&b,&t,&l);
  137.            }
  138.            endcons();
  139.            tree(i,in,d,fn,fs,b,t,l);
  140.            pr = p;
  141.            fclose(fptr1);        
  142. }
  143.  
  144.  
  145. createnode(fptr1,i,in,d,fn,fs,b,t,l)    /* restores nodes for parse tree */
  146. FILE *fptr1;
  147. int i,in,d,fn,fs,b,t,l;
  148. {
  149.           int j;
  150.           char w[MAXLINE];
  151.           struct tnode *allocp();
  152.  
  153.           if (d == NEWINSTR && pr->instr != DEF) {
  154.               i = reloc[i];                   /* set up array to convert */
  155.               j = 0;                              /* old symb tab to new */
  156.               while (new[i].loc[j] != NULL)
  157.                      j++;
  158.               new[i].loc[j] = p;
  159.               new [i].loc[++j] = NULL;
  160.               if (l != 15)
  161.                  l = last;
  162.               tree(i,in,d,fn,fs,b,t,l);
  163.               p = p->next = allocp();
  164.           } else switch (i) { 
  165.                  case 0:            /* move */
  166.                  case 1:            /* turnleft */
  167.                  case 2:            /* pickbeeper */
  168.                  case 3:            /* putbeeper */
  169.                  case 4:            /* turnoff */
  170.                     while (in < pr->indent)
  171.                            endcons();
  172.                     if (l != 15)
  173.                        l = last;
  174.                     tree(i,in,d,fn,fs,b,t,l);
  175.                     p = p->next = allocp();
  176.                     break;
  177.                 case 5:             /* BEGIN */
  178.                 case 7:             /* BEGINNING-OF-EXECUTION */
  179.                 case 13:            /* ELSE */
  180.                    if (l != 15)
  181.                       l = last;
  182.                    tree(i,in,d,fn,fs,b,t,l);
  183.                    p = p->sub = allocp();
  184.                    break;
  185.                case 8:              /* END */
  186.                   if (l != 15)
  187.                      l = last;
  188.                   tree(i,in,d,fn,fs,b,t,l);
  189.                   p = p->next = allocp();
  190.                   endcons();
  191.                   break;
  192.                case 11:             /* IF */
  193.                   while (in < pr->indent)
  194.                       endcons();
  195.                   if (l != 15)
  196.                      l = last;
  197.                   tree(i,in,d,fn,fs,b,t,l);
  198.                   p = p->sub = allocp();
  199.                   fscanf(fptr1,"%d %d %d %d %d %d %d %d",
  200.                          &i,&in,&d,&fn,&fs,&b,&t,&l);         /* get test */
  201.                   if (l != 15)
  202.                      l = last;
  203.                   tree(i,in,d,fn,fs,b,t,l);
  204.                   p = p->sub = allocp();
  205.                   fscanf(fptr1, "%d %d %d %d %d %d %d %d",
  206.                          &i,&in,&d,&fn,&fs,&b,&t,&l);         /* get THEN */
  207.                   if (l != 15)
  208.                      l = last;
  209.                   tree(i,in,d,fn,fs,b,t,l);
  210.                   p = p->sub = allocp();
  211.                   break;
  212.                case 14:               /* ITERATE */
  213.                case 15:               /* WHILE */
  214.                   while (in < pr->indent)
  215.                       endcons();
  216.                   if (l != 15)
  217.                      l = last;
  218.                   tree(i,in,d,fn,fs,b,t,l);
  219.                   p = p->sub = allocp();
  220.                   fscanf(fptr1,